package 力扣_数学思维题;

/**1109. 航班预订统计
 * @author zx
 * @create 2022-04-28 10:25
 */
public class Num1109 {
    /**
     * @return 差分数组
     */
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] res = new int[n];
        int first = 0;
        int last = 0;
        int seats = 0;
        for(int i = 0;i < bookings.length;i++) {
            first = bookings[i][0];
            last = bookings[i][1];
            seats = bookings[i][2];
            //对区间[first, last]增加seats
            res[first - 1] += seats;
            if (last < n) {
                res[last] -= seats;
            }
        }
        //得到更新后的数组
        for (int i = 1; i < n; i++) {
            res[i] += res[i - 1];
        }
        return res;
    }

    /**
     * @return 暴力解法(时间复杂度太高)
     */
    public int[] corpFlightBookings2(int[][] bookings, int n) {
        int[] res = new int[n];
        int first = 0;
        int last = 0;
        int seats = 0;
        for(int i = 0;i < bookings.length;i++){
            for(int j = 0;j < 3;j++){
                first = bookings[i][0];
                last = bookings[i][1];
                seats = bookings[i][2];
            }
            for(int k = first;k <= last;k++){
                res[k - 1] += seats;
            }
        }
        return res;
    }
}
